// pages/goodsdetail/goodsdetail.js
const api = require("../../config/api");
const util = require("../../utils/utils")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    info: {}, //商品数据
    attribute: [], //商品参数
    issue: [], //常见问题
    comment: [], //评论
    brand: {}, //制造商
    specificationList: [], //规格
    productList: [], // 一个商品对应不同的产品
    retailPrice: 0, //商品价格,
    content: "",
    guige: '请选择规格数量',
    relatedGoods: [],
    show: false,
    choseArr: []
  },
  clickChoose(e) {
    //console.log(e.target.dataset.itemname, e.target.dataset.chooseval);
    let that = this;
    let tempArr = [];
    let tagValue = '';
    if (that.data.choseArr.length == 0) {
      tempArr = that.data.specificationList.map(item => {
        return {
          title: item.name,
          value: ''
        }
      });
    }
    else {
      tempArr = that.data.choseArr
    }

    let idx = tempArr.findIndex(item1 => item1.title == e.target.dataset.itemname)
    //console.log(idx);
    tempArr[idx].value = e.target.dataset.chooseval;
    tempArr.forEach(item2 => {
      tagValue += item2.value
    })
    that.setData({
      choseArr: tempArr,
      guige: tagValue
    });

  },
  popCancel() {
    this.setData({ show: false });
  },
  popConfirm() {
    let flag = false;
    flag = this.data.choseArr.length > 0 && this.data.choseArr.every(item => { return item.value != '' });
    if (flag) {
      console.log('添加购物车接口');
      this.setData({ show: false });
    }
    else {
      wx.showToast({
        icon: 'none',
        title: '规格选择不完整',
      })
    }
  },
  onChange(e) {
    console.log(e.detail);
  },
  onClose() {
    this.setData({ show: false });
  },
  onClickButton() {
    this.setData({ show: true });
  },

  getGoodsRelated: async function () {
    let resp = await util.request(api.GoodsRelated, {
      id: this.data.id
    });
    if (resp.errno == 0) {
      this.setData({
        relatedGoods: resp.data.goodsList
      })
    }
  },
  getGoodsInfo() {
    let that = this;
    util.request(api.GoodsDetail, {
      id: that.data.id
    }).then(res => {
      if (res.errno == 0) {
        //console.log(res);
        let { info, attribute, issue, comment, brand, specificationList, productList } = res.data;
        that.setData({
          info,
          attribute,
          issue,
          comment,
          brand,
          specificationList,
          productList,
          retailPrice: info.retailPrice,
          content: info.detail.replace(/\<img/gi, "<img class='diy-img'")
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //console.log(options.id);
    if (options.id) {
      this.setData({
        id: parseInt(options.id)
      })
    }
    this.getGoodsInfo();
    this.getGoodsRelated()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})